package com.ssm.controller;

import com.ssm.annotation.SysLogMethod;
import com.ssm.pojo.SysLog;
import com.ssm.service.UserService;
import com.ssm.shrio.ShiroUtils;
import com.ssm.util.EncoderByMd5;

import com.ssm.util.ResultData;
import com.ssm.validator.Assert;
import org.aspectj.lang.JoinPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.security.NoSuchAlgorithmException;

/**
 * Created by 墨殇
 * 2017/12/27.
 * @author Administrator
 */
@Controller
@RequestMapping("sys/user")
public class UserController extends AbstractController{
    @Autowired
    private UserService userService;

    /**
     * 获取登录的用户信息
     */
    @ResponseBody
    @RequestMapping("/info")
    public ResultData info(){
        return ResultData.ok().put("user", getUser());
    }
    /**
     * 修改登录用户密码
     */
    @ResponseBody
    @SysLogMethod(operationName = "修改登录密码",operationType = "修改密码")
    @RequestMapping("updatePassword")
    public ResultData updatePassword(String password, String newPassword,SysLog sysLog){
        Assert.isBlank(newPassword, "新密码不为能空");

        try {
            password =  EncoderByMd5.EncoderByMd5(password);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        //sha256加密
        try {
            newPassword = EncoderByMd5.EncoderByMd5(newPassword);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        //更新密码
        Integer count = userService.updatePassword(getUserId(), password, newPassword,sysLog);
        if(count == 0){
                sysLog.setIsSuccess(0);
            sysLog.setRemark("(原密码不正确)");
            return ResultData.error("原密码不正确");
        }else {
            sysLog.setIsSuccess(1);
            sysLog.setRemark("(修改成功)");
            }
//        //退出
//        ShiroUtils.logout();
          return  ResultData.ok();

    }


}
